package com.qianjiangcloud.store.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.activity.AgentActivity;
import com.qianjiangcloud.store.adapter.GalleryAdapter;
import com.qianjiangcloud.store.ui.FlowView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.libary.cache.MyImageLoader;

/**
 * 横向滑动的商品展示
 */
public class SlidingGoodsUtils {
    private Point mScreenSize = null;
    private RelativeLayout adsFrameLayout = null;
    private View vi = null;
    private FlowView mTopAdsView;
    private Context context;
    private String isshow_module_title;
    private RecyclerView mRecyclerView;
    LinearLayout more;

    public View getSlidGoodsViews(Context mContext) {
        context = mContext;
        //创建布局
        adsFrameLayout = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams adsFrameLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        adsFrameLayout.setLayoutParams(adsFrameLayoutParams);
        vi = LayoutInflater.from(mContext).inflate(R.layout.sliding_goods_view, null);
        mRecyclerView = (RecyclerView) vi.findViewById(R.id.goods_recyclerview_horizontal);
        //创建item布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        adsFrameLayout.addView(vi);
        return adsFrameLayout;
    }

    public void setUpView(Context mContext, String json_str, String isshow_sub_module,String isshow_module_title, String title, String title_color, String title_icon, View view) {
        //判断是否有更多的头部布局
        context=mContext;
        more = (LinearLayout) view.findViewById(R.id.look_more);
        RelativeLayout isshow_title = (RelativeLayout) view.findViewById(R.id.isshow_title);
        if (isshow_sub_module.equals("true")) {
            more.setVisibility(View.VISIBLE);
        }
        if (isshow_module_title.equals("false")) {
            isshow_title.setVisibility(View.GONE);
        }
        TextView title_name = (TextView) view.findViewById(R.id.title);
        ImageView icon = (ImageView) view.findViewById(R.id.image_icon);
        ImageView title_image = (ImageView) view.findViewById(R.id.image);
        if (null != title) {
            title_name.setText(title);
        }
        if (null != title_color) {
            title_name.setTextColor(Color.parseColor(title_color));
            icon.setBackgroundColor(Color.parseColor(title_color));
        }
        if (null != title_icon) {
            MyImageLoader.displayDefaultImage(title_icon, title_image);
        }
        this.mRecyclerView = (RecyclerView) view.findViewById(R.id.goods_recyclerview_horizontal);
        parseHomeAdsJson(json_str, mContext);
    }

    /**
     * 解析广告json
     */
    private void parseHomeAdsJson(String json_str, final Context mContext) {
        final ArrayList<JSONObject> mSlidingGoodsArray = new ArrayList<JSONObject>();
        try {
            JSONObject all = new JSONObject(json_str);
            if (Run.checkRequestJson(mContext, all)) {
                JSONObject infoObject = all.getJSONObject("data");
                JSONArray child = infoObject.optJSONArray("info");
                if (child != null && child.length() > 0) {
                    mSlidingGoodsArray.clear();
                    for (int i = 0; i < child.length(); i++) {
                        JSONObject topJson = child.getJSONObject(i);
                        mSlidingGoodsArray.add(topJson);
                    }
                    if (mSlidingGoodsArray.size() > 0) {
                        reloadMainView(false, mContext, mSlidingGoodsArray);
                    } else {
                        reloadMainView(true, mContext, mSlidingGoodsArray);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(AgentActivity.intentForFragment(
                        mContext, AgentActivity.FRAGMENT_GOODS_LIST)
                        .putExtra("module_id",mSlidingGoodsArray.get(0).optString("module_id")
                        ).putExtra("isMore",true));
            }
        });
    }

    private void reloadMainView(boolean isOutDate, Context mContext, ArrayList<JSONObject> mSlidingGoodsArray) {
        if (mSlidingGoodsArray != null && mSlidingGoodsArray.size() > 0 && !isOutDate) {
//         添加横向滑动的RecycleView
            mRecyclerView.setAdapter(new GalleryAdapter(mContext, mSlidingGoodsArray));
        } else {
            adsFrameLayout.setVisibility(View.GONE);
        }
    }


}
